<template>
	<view >
		<view class="set-box">
			<u-form label-position="top">
				<u-form-item label="客户:"  label-width="180">
					<u-input v-model="customer.name" :border="true" disabled placeholder="客户名称" />
				</u-form-item>
				<u-form-item label="成交情况:"  label-width="180">
					<view class="u-flex">
						<u-tag class="u-m-r-15" bg-color="#605ca8" :text=" '总消费：' + purchaseTotalFormat " mode="dark" />
						<u-tag class="u-m-r-15" bg-color="#0073b7" :text=" '下单：' + customer.purchase_times + '次' " mode="dark" />
						<u-tag class="u-m-r-15" bg-color="#18bc9c" :text=" (customer.purchase_times == 0 ? '无' : $u.timeFrom(customer.deal_time, false)) + '下单'" mode="dark" />
					</view>
				</u-form-item>
				<u-form-item label="成交金额:" required  label-width="180" >
					<u-input v-model="money" :border="true" type="number" placeholder="请输入成交金额" />
				</u-form-item>
			</u-form>
			<view class="u-m-t-80" style="text-align: center;">
				<u-button class="u-m-l-15" type="success"  @click="onSubmit" :custom-style="{backgroundColor: vuex_theme.color, color: vuex_theme.bgColor}" :ripple="true">提交</u-button>
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				customer: {},
				customer_id: {},
				money: '',
			};
		},
		computed: {
			purchaseTotalFormat(){
				if(this.customer.purchase_total) {
					let num;
					if (this.customer.purchase_total > 9999) { //大于9999显示x.xx万
						num = (Math.floor(this.customer.purchase_total / 100) / 100) + '万';
					} else if (this.customer.purchase_total <= 9999 && this.customer.purchase_total > -9999) {
						num = this.customer.purchase_total 
					}
					return num;
				} else {
					return 0;
				}
			}
		},
		// 只有onReady生命周期才能调用refs操作组件
		onReady() {
			
		},
		onLoad(e) {
			this.customer_id = e.customer_id ? e.customer_id: ''
			this.getData()
		},
		methods: {
			// 获取客户详情
			getData() {
				this.$u.api.getCustomer({id: this.customer_id}).then(res => {
					if(res.code == 1 ) {
						this.customer = res.data
					}
				})
			},
			// 开始提交
			onSubmit(){
				if(this.money == ''){
					// 提示
					uni.showToast({
						title: '请填写成交金额',
						icon: 'none',
						duration: 2000
					})
					return
				}
				// 数据验证
				let obj = {
					ids: this.customer_id,
					money: this.money,
				}
				this.$u.api.onDealCustomer(obj).then((res) => {
					console.log(res)
					if(res.code == 1){
						// 提示
						this.$refs.uToast.show({
							title: res.msg,
						})
						setTimeout(() => {
							uni.navigateBack();
						}, 1500);
					}
				})
			},
		},
	}
</script>

<style lang="scss">
.set-box {
	padding: 0rpx 22rpx;
	margin-bottom: 80rpx;
}
</style>
